package com.sntk.organauthomanager.infrastructure.convert;

import com.sntk.organauthomanager.domain.model.RoleIdentify;
import com.sntk.organauthomanager.domain.model.UserRole;
import com.sntk.organauthomanager.infrastructure.db.entity.UserRoleEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @ClassName: UserRoleConvert
 * @author: wgchao
 * @createTime: 2020/7/21 6:24 PM
 * @Description: TODO
 */
@Component
public class UserRoleConvert {
    public UserRole convert(List<UserRoleEntity> userRoleEntities){
        if(userRoleEntities==null || userRoleEntities.isEmpty()){
            return null;
        }
        Set<RoleIdentify> roleIdentifies = new HashSet<>();
        for(UserRoleEntity userRoleEntity : userRoleEntities){
            roleIdentifies.add(new RoleIdentify(userRoleEntity.getInstitutionNumber(), userRoleEntity.getRoleCode()));
        }
        return new UserRole(null, userRoleEntities.get(0).getUname(), roleIdentifies);
    }

    public List<UserRoleEntity> convert(UserRole userRole){
        if(userRole.getRoleIdentifies()==null || userRole.getRoleIdentifies().isEmpty()){
            return null;
        }
        List<UserRoleEntity> userRoleEntities = new ArrayList<>();
        for(RoleIdentify roleIdentify : userRole.getRoleIdentifies()){
            UserRoleEntity userRoleEntity = new UserRoleEntity();
            userRoleEntity.setUname(userRole.getUname());
            userRoleEntity.setInstitutionNumber(roleIdentify.getInstitutionNumber());
            userRoleEntity.setRoleCode(roleIdentify.getRoleCode());
            userRoleEntities.add(userRoleEntity);
        }
        return userRoleEntities;
    }
}
